package com.atty.dms.controller;

import com.atty.dms.common.convention.result.Result;
import com.atty.dms.common.convention.result.Results;
import com.atty.dms.dto.req.UserLoginReqDTO;
import com.atty.dms.dto.req.UserRegisterReqDTO;
import com.atty.dms.dto.req.UserUpdateReqDTO;
import com.atty.dms.dto.resp.UserLoginRespDTO;
import com.atty.dms.dto.resp.UserRespDTO;
import com.atty.dms.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:UserController
 * Package:com.atty.dms.controller
 * Description:
 *
 * @Author:tony
 * @Create :2025/4/6 16:02
 * @Version :1.0
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;


    /**
     *根据用户名查询用户信息
     */
    @GetMapping("/api/dms/admin/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        return Results.success(userService.getUserByUsername(username));
    }

    @GetMapping("/api/dms/admin/user/userId/{userId}")
    public Result<UserRespDTO> getUserByUserId(@PathVariable("userId") Long userId){
        return Results.success(userService.getUserByUserId(userId));
    }

    /**
     * 查询用户名是否可用
     */
    @GetMapping("/api/dms/admin/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(!userService.hasUsername(username));
    }

    /**
     * 注册用户
     */
    @PostMapping("/api/dms/admin/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     *  更新用户信息
     */
    @PutMapping("/api/dms/admin/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    /**
     *  用户登录
     */
    @PostMapping("/api/dms/admin/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }

    /**
     *  检查用户登录
     */
    @GetMapping("/api/dms/admin/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/dms/admin/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

    /**
     * 分页获取所有用户信息
     */
    @GetMapping("/api/dms/admin/user/page")
    public Result<Page<UserRespDTO>> getAllUsersByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int PageSize) {
        return Results.success(userService.getAllUsersByPage(pageNum, PageSize));
    }

    /**
     * 分页获取不同性别的未入住学生信息
     */
    @GetMapping("/api/dms/admin/user/page/gender")
    public Result<Page<UserRespDTO>> getAllUsersByGenderAndNotInDormitoryByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int PageSize,
            @RequestParam (value = "gender") int gender) {
        return Results.success(userService.getAllUsersByGenderAndNotInDormitoryByPage(pageNum, PageSize,gender));
    }


}
